﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Threading;
using Net.Fly.Article.Util;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;

namespace Net.Fly.Article.AutoSubmit
{
    public class SelfgrowthSubmit : AbstractSubmit
    {
        private String m_rootURL = "http://www.selfgrowth.com";
        private String m_memberURL = "";
        private String m_userName = "brihgt.lee@gmail.com";
        private String m_password = "bright820813";
        private String m_loginURL = "http://www.selfgrowth.com/user";
        private String m_submitURL = "http://www.selfgrowth.com/node/add/article";
        private String m_mainCatagory = "Money & Careers";
        private String m_subCatagory = "Network Marketing and MLM's";
        private String m_title = "Some tips of moving to cloud";
        private String m_article = "Once the organization's assets are identified and classified, the security chains<br>should be defined and put into place.<br>A security chain must protect the organization's information assets at all levels,<br>including physical security, technical security, and procedural and legal steps.<br>Physical security includes measures such as restricting access to data centers,<br>shredding paper documents and destroying tapes and hard drives. Technical<br>security includes everything from the basics of firewalls and access control<br>systems to more advanced techniques such as disabling USB ports. Finally,<br>procedures for handling information assets must be clearly defined and<br>adequately explained to all employees of the organization. In some cases, the<br>procedures may include legal requirements such as laws covering the retention<br>or destruction of data.<br>Once the organization has classified its information assets and defined the risks<br>and requirements for using them, the decision to move to the cloud will be more<br>straightforward.";
        private String m_bio = "I'm a software engineer with more than 10 years development experience";
        private String m_author = "Bright";
        private String m_errMessage = "";

        private SubmitStatus m_status = SubmitStatus.NEW;

        private IWebDriver m_driver;


        public SelfgrowthSubmit()
        {
            m_driver = new FirefoxDriver();
        }

        public override bool Login()
        {
            m_status = SubmitStatus.LOGINING;

            try
            {
                m_driver.Navigate().GoToUrl(m_loginURL);

                IWebElement userInput = WebDriverExtension.FindElement(m_driver, By.Id("edit-name"), 5);// m_driver.FindElement(By.XPath("//label/input[@name='email']"));
                userInput.SendKeys(m_userName);

                IWebElement passwordInput = WebDriverExtension.FindElement(m_driver, By.Id("edit-pass"), 5);  // m_driver.FindElement(By.XPath("//label/input[@name='password']"));
                passwordInput.SendKeys(m_password);

                IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.Id("edit-submit"), 5);  //m_driver.FindElement(By.XPath("//form/input[@value='Login']"));
                submitButton.Submit();
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
                return false;
            }
            
            return true;
        }

        public override bool Submit()
        {
            m_status = SubmitStatus.SUBMITTING;
            try
            {
                m_driver.Navigate().GoToUrl(m_submitURL);

                // Select main Catagory
                SelectElement catagoryMain = new SelectElement(WebDriverExtension.FindElement(m_driver, By.Id("edit-field-article-topic-0-topic-group"), 5));
                catagoryMain.SelectByText(m_mainCatagory);
                SelectElement catagorySub = new SelectElement(WebDriverExtension.FindElement(m_driver, By.Id("edit-field-article-topic-0-value"), 5));
                Thread.Sleep(5000);
                catagorySub.SelectByText(m_subCatagory);

                // Set title
                IWebElement titleInput = WebDriverExtension.FindElement(m_driver, By.Id("edit-title"), 5);
                titleInput.SendKeys(m_title);

                // Set article body
                IWebElement bodyInput = WebDriverExtension.FindElement(m_driver, By.Id("edit-body"), 5);
                bodyInput.SendKeys(m_article);

                // Set bio
                IWebElement bioInput = WebDriverExtension.FindElement(m_driver, By.Id("edit-field-article-bio-0-value"), 5);
                bioInput.SendKeys(m_bio);

                // Set author name
                IWebElement authorInput = WebDriverExtension.FindElement(m_driver, By.Id("edit-field-article-author-0-value"), 5);
                authorInput.Clear();
                authorInput.SendKeys(m_author);

                // Submit
                IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.Id("edit-submit"), 5);
                submitButton.Click();


                // check error message
                IWebElement errBox = WebDriverExtension.FindElement(m_driver, By.XPath("//div[@class='messages error']"), 5);
                if (errBox != null)
                {

                    m_errMessage = errBox.Text;
                    m_status = SubmitStatus.ERROR;
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
                return false;
            }

            m_status = SubmitStatus.SUCCESS;
            return true;
        }

        public override String GetArticleURL()
        {
            if (m_status != SubmitStatus.SUCCESS)
            {
                return "";
            }

            try
            {
               // m_driver.Navigate().GoToUrl(m_memberURL);

                // find new article
                ReadOnlyCollection<IWebElement> hrefs = WebDriverExtension.FindElements(m_driver, By.XPath("//td[@class='views-field views-field-title']/a"), 5);
                foreach (IWebElement ele in hrefs)
                {
                    if (ele.Text.Contains(m_title))
                        return ele.GetAttribute("href");
                }
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
            }

            return "";
        }
    }
}
